{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-07-12T10:18:08.541385Z",
     "start_time": "2018-07-12T10:18:08.213839Z"
    }
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "import torch.nn.functional as F\n",
    "\n",
    "def ConvLayer(in_channels, out_channels, kernel_size=3, stride=1, \n",
    "    upsample=None, instance_norm=True, relu=True):\n",
    "    layers = []\n",
    "    if upsample:\n",
    "        layers.append(nn.Upsample(mode='nearest', scale_factor=upsample))\n",
    "    layers.append(nn.ReflectionPad2d(kernel_size // 2))\n",
    "    layers.append(nn.Conv2d(in_channels, out_channels, kernel_size, stride))\n",
    "    if instance_norm:\n",
    "        layers.append(nn.InstanceNorm2d(out_channels))\n",
    "    if relu:\n",
    "        layers.append(nn.ReLU())\n",
    "    return layers\n",
    "\n",
    "class ResidualBlock(nn.Module):\n",
    "    def __init__(self, channels):\n",
    "        super(ResidualBlock, self).__init__()\n",
    "        self.conv = nn.Sequential(\n",
    "            *ConvLayer(channels, channels, kernel_size=3, stride=1), \n",
    "            *ConvLayer(channels, channels, kernel_size=3, stride=1, relu=False)\n",
    "        )\n",
    "\n",
    "    def forward(self, x):\n",
    "        return self.conv(x) + x\n",
    "\n",
    "class TransformNet(nn.Module):\n",
    "    def __init__(self, base=32):\n",
    "        super(TransformNet, self).__init__()\n",
    "        self.downsampling = nn.Sequential(\n",
    "            *ConvLayer(3, base, kernel_size=9), \n",
    "            *ConvLayer(base, base*2, kernel_size=3, stride=2), \n",
    "            *ConvLayer(base*2, base*4, kernel_size=3, stride=2), \n",
    "        )\n",
    "        self.residuals = nn.Sequential(*[ResidualBlock(base*4) for i in range(5)])\n",
    "        self.upsampling = nn.Sequential(\n",
    "            *ConvLayer(base*4, base*2, kernel_size=3, upsample=2),\n",
    "            *ConvLayer(base*2, base, kernel_size=3, upsample=2),\n",
    "            *ConvLayer(base, 3, kernel_size=9, instance_norm=False, relu=False),\n",
    "        )\n",
    "    \n",
    "    def forward(self, X):\n",
    "        y = self.downsampling(X)\n",
    "        y = self.residuals(y)\n",
    "        y = self.upsampling(y)\n",
    "        return y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-07-12T10:18:14.609117Z",
     "start_time": "2018-07-12T10:18:08.543466Z"
    }
   },
   "outputs": [],
   "source": [
    "device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n",
    "transform_net = TransformNet(32).to(device)\n",
    "transform_net.load_state_dict(torch.load('transform_net.pth'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-07-12T10:18:14.616276Z",
     "start_time": "2018-07-12T10:18:14.611411Z"
    }
   },
   "outputs": [],
   "source": [
    "X = torch.rand((1, 3, 256, 256)).to(device)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-07-12T10:18:19.447909Z",
     "start_time": "2018-07-12T10:18:14.617999Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 4.58 s, sys: 236 ms, total: 4.81 s\n",
      "Wall time: 4.82 s\n"
     ]
    }
   ],
   "source": [
    "%%time \n",
    "for i in range(1000):\n",
    "    out = transform_net(X)\n",
    "    del out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": "block",
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": "40",
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "position": {
    "height": "441px",
    "left": "934px",
    "right": "20px",
    "top": "120px",
    "width": "333px"
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
